#! /usr/bin/python
# -*- coding: cp1250 -*-
#sys.path.append(r'C:\AFT\.pylib')

import glob, sys, os, re, string
import pepe.p_txt.bigtxt as bt
#import pepe.p_string.cp1250ascii as psc
import pepe.p_string.utf8ascii as psc
from pepe.glib import *

#sas variable name can not contain '/' and mus be les then 32 char long
sas_variable = lambda x_s: re.sub('_{1,}$','',upper_clean(string.strip(re.sub('/','_',x_s)[:30])))+'_'
upper_clean = lambda x_s: re.sub('\W','_',psc.uppercase(string.strip(x_s)))

def csvsas(inpfile, hasHeader='0', headerCustom=''):
    '''
    Purpose:    Create SAS data load sections from csv file.
    Syntax:     csv_sas <inpfile> <has-header> <custom-output-header>
    Example:    csv_sas.py gl.dat 1 \"\"
    '''
    #print string.join([inpfile, hasHeader, headerCustom], ';')

    # 2010-10-22_2257 vp
    ## using underscore instead dot because .# utilities 
    ## cut out everything after dot and then it is not clear 
    ## whether utility was run on original file or on output
    ## of other .# utility that processed given file
    #out_fl = open(inpfile+'.csv_sas.#','w')
    out_fl = open(inpfile+'_csv_sas.#','w')

    #for inf_s in glob.glob(inpfiles):
    out_h = {}
    flcnt_n = 0
    header_l = []
    columns_count = []

    cdelimiter = ''
    
    for line_s in bt.lines(inpfile):

        # find out column delimiter
        if cdelimiter == '': cdelimiter = findcoldelimiter(line_s)

        flcnt_n += 1
        #print cdelimiter
        line_s = re.sub('\n', '', line_s)
        columns_l = string.split(line_s, cdelimiter)
        #if flcnt_n > 439070 : print '###\n', flcnt_n, len(columns_l), line_s, columns_l
        if len(columns_l) not in columns_count: columns_count.append(len(columns_l))
        if len(columns_count) > 1:
            print line_s
            print columns_count
            print len(columns_l)
            print "!!! Different columns count on line: %s"%(str(flcnt_n))
            sys.exit(2)
        #print columns_count, len(columns_count)
        for index, value in enumerate(columns_l):
            #print index, value
            cindex = 'c'+string.zfill(index+1,3)
            if  hasHeader == "1" and headerCustom == "" and flcnt_n == 1:
                header_l = columns_l
            elif hasHeader == "1" and headerCustom <> "" and flcnt_n == 1:
                header_l = string.split(headerCustom, cdelimiter)
            elif hasHeader == "0" and headerCustom <> "" and flcnt_n == 1:
                header_l = string.split(headerCustom, cdelimiter)
            else:
                if out_h.get(cindex):
                    if  out_h[cindex][0] < len(str(value)):
                        out_h[cindex][0] = len(str(value))
                else:
                    if len(header_l) > 1:
                        out_h[cindex] = [len(str(value)),header_l[index], value]
                    else:
                        out_h[cindex] = [len(str(value)),'',value]

    template_s = """        %orig_name%     FORMAT = $%col_len%."""
    for i in sorted(out_h.keys()):
        
        out_s = re.sub('%orig_name%'    , string.ljust(sas_variable(out_h[i][1]),45), template_s)
        if out_h[i][0] == 0: out_s = re.sub('%col_len%'      ,'1', out_s)
        else: out_s = re.sub('%col_len%'      , str(out_h[i][0]), out_s)
        out_fl.write(out_s + '\n')
        out_s = ''

    #out_fl.write('AccountNum_   AccountName_  Description_  Description2_ Username_  Jrnl_Source_MvsS_ \n')
    #out_fl.write('DateEffective_ DateDocument_ DatePosted_ DateModified_ TimeModified_  \n')
    #out_fl.write('AmtDr_  AmtCr_  AmtDrC_  AmtCrC_ DC_Indicator_  AccountGroup_  Currency_ Record_Orig_Id_ \n')
    out_fl.write('\n\n')
    
    ###     CURRENCY_                                         = SCAN(in,2,'^');
    template_s = """    %orig_name%     = SCAN(in,%col_no%,'%cdelimiter%');"""
    for i in sorted(out_h.keys()):
        out_s = re.sub('%orig_name%'    , string.ljust(sas_variable(out_h[i][1]),45), template_s)
        out_s = re.sub('%col_no%'      , str(int(i[1:])), out_s)
        out_s = re.sub('%cdelimiter%'  , cdelimiter, out_s)
        out_fl.write(out_s + '\n')
        out_s = ''

    out_fl.write('\n\n')

    ###    CURRENCY_                                     -- CZK
    template_s = """    %orig_name% -%col_no%- %orig_value%"""
    for i in sorted(out_h.keys()):
        out_s = re.sub('%orig_name%'    , string.ljust(sas_variable(out_h[i][1]),45), template_s)
        out_s = re.sub('%col_no%'       , string.zfill(str(int(i[1:])), 2), out_s)
        out_s = re.sub('%orig_value%'   , str(out_h[i][2]), out_s)
        out_fl.write(out_s + '\n')
        out_s = ''

    out_fl.write('\n\n')
    
    ### drop ... section 
    template_s = """ %orig_name%"""
    out_s = ''
    for i in sorted(out_h.keys()):
        out_s += '        ' + sas_variable(out_h[i][1])+ '\n'
    out_fl.write('    drop \n        in\n\n' + out_s + '    ;\n')
    out_s = ''

    out_fl.close()
    return 1

if __name__ == "__main__":
    #csvsas("gl1.dat", "1", "")
    #print sys.argv
    #print os.getcwd()  os.getcwd()+'\\'+

    if len(sys.argv) != 4:
        print csvsas.__doc__
    else:
        csvsas(sys.argv[1], sys.argv[2], sys.argv[3])